package com.yiqi.lottery.feature.lottery.view;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.*;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.CouprceBo;
import com.yiqi.lottery.entity.bo.HallBo;
import com.yiqi.lottery.feature.lottery.contract.HallContract;
import com.yiqi.lottery.feature.lottery.dependencies.hall.DaggerHallComponent;
import com.yiqi.lottery.feature.lottery.dependencies.hall.HallPresenterModule;
import com.yiqi.lottery.feature.lottery.view.adapter.BuyHallAdapter;

import javax.inject.Inject;

public class BuyTogetherActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, HallContract.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, View.OnClickListener, RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.btn_gendan)
    TextView mBtnGendan;
    @BindView(R.id.img_xiala)
    ImageView mImgXiala;
    @BindView(R.id.btn_filter)
    RelativeLayout mBtnFilter;
    @BindView(R.id.view_toobar)
    RelativeLayout mViewToobar;
    @BindView(R.id.rv_buy_hall)
    RecyclerView mRvBuyHall;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rg_hall_filter)
    RadioGroup mRgHallFilter;
    @BindView(R.id.view_left)
    View mViewLeft;
    @BindView(R.id.view_center)
    View mViewCenter;
    @BindView(R.id.view_right)
    View mViewRight;
    private int mCurrentBetType = 0;

    PopupWindow mPopupWindow;
    private String label;
    BuyHallAdapter mAdapter;

    @Inject
    HallContract.Presenter mPresenter;
    private EmptyView mEmptyView;
    PageData<CouprceBo> mData;
    private int pageSize = 10;
    private int mCurrentCounter = 0;
    private int pageIndex = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private Integer mBetType=null;
    private int mSort=3;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_buy_together;
    }

    @Override
    protected void initInjector() {
        DaggerHallComponent.builder().hallPresenterModule(new HallPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initAdapter();

    }

    @Override
    protected void initData() {
        switch (mCurrentBetType) {
            case 0:
                mBetType=0;
                label = "all";
                break;
            case 1:
                mBetType=4;
                label = "football";
                break;
            case 2:
                mBetType=5;
                label = "basketball";
                break;
            case 3:
                mBetType=2;
                label = "sfc";
                break;
            case 4:
                mBetType=null;
                label = "jqc";
                break;
            case 5:
                mBetType=3;
                label = "rx9";

                break;
            case 6:
                mBetType=null;
                label = "6db";
                break;
        }
        pageIndex = 0;
        mPresenter.getHallList(pageIndex,mSort,mBetType);
    }

    @Override
    protected void initEvent() {
        mRgHallFilter.setOnCheckedChangeListener(this);
    }

    @OnClick({R.id.btn_back, R.id.btn_filter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_filter:
                if (mPopupWindow != null) {
                    mPopupWindow.dismiss();
                } else {
                    mImgXiala.setRotation(180);
                    initPopupWindowView();
                }
                break;
        }
    }

    private void initAdapter() {
        mAdapter = new BuyHallAdapter();
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        mAdapter.isFirstOnly(false);
        mEmptyView = new EmptyView(mRvBuyHall);
        mEmptyView.setErrorButtonClickListener(this);
        mRvBuyHall.setLayoutManager(new LinearLayoutManager(this));
        mRvBuyHall.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter.bindToRecyclerView(mRvBuyHall);
        mAdapter.setOnItemClickListener(this);
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableLoadMore(false);
        mAdapter.setOnLoadMoreListener(this);

    }

    public void initPopupWindowView() {
        LinearLayout contentView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.popup_buy_togther, null, false);
        mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.showAsDropDown(mViewToobar);
        // 设置背景半透明
        backgroundAlpha(0.7f);

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                mImgXiala.setRotation(0);
                mPopupWindow = null;
            }
        });

        contentView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
            }
        });
        resetBtnByCurrentBetType(contentView);
    }

    private void resetBtnByCurrentBetType(LinearLayout contentView) {
        int count = contentView.getChildCount();
        RadioGroup radioGroup;
        RadioButton btn;
        for (int i = 0; i < count; ++i) {
            radioGroup = (RadioGroup) contentView.getChildAt(i);
            for (int j = 0; j < 4; ++j) {
                int tag = i * 4+ j;
                btn = (RadioButton) radioGroup.getChildAt(j);
                btn.setTag(tag);
                btn.setChecked(tag == mCurrentBetType);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mCurrentBetType = (int) view.getTag();
                        mPopupWindow.dismiss();
                        mPopupWindow = null;
                        mBtnGendan.setText(((RadioButton) view).getText());
                        initData();
                    }
                });
            }
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        getWindow().setAttributes(lp);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, BuyHallDetailActivity.class);
        intent.putExtra("id", mData.getList().get(position).getCopurce_id() );
        intent.putExtra("type", true);
        startActivity(intent);
    }

    @Override
    public void showHallListSuccess(PageData<CouprceBo> stringPageData) {
        mRefreshLayout.finishRefresh();
        mData = stringPageData;
        mAdapter.setNewData(mData.getList());
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd();
        }
        isErr = false;
        mCurrentCounter = pageSize;
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showHallListMoreSuccess(PageData<CouprceBo> hallBoPageData) {
        isErr = false;
        mAdapter.addData(hallBoPageData.getList());
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.loadMoreComplete();
    }

    @Override
    public void showHallListMoreError(String ex) {
        isErr = true;
        mAdapter.loadMoreFail();
    }

    @Override
    public void showHallListError(String ex) {
        mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView("此彩种暂未开放!"));
        mAdapter.setNewData(null);
    }

    @Override
    public void showHallListEmpty() {
        mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void showHallDetailSuccess(CouprceBo msg) {

    }

    @Override
    public void showCopurchSubmitSuccess(String msg) {

    }

    @Override
    public void showCopurchPaySuccess(String msg) {

    }

    @Override
    public void showCancleSuccess() {

    }

    @Override
    public void showCancleError(String ex) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        initData();
    }

    @Override
    public void onLoadMoreRequested() {

            if (mAdapter.getData().size() < pageSize) {
                mAdapter.loadMoreEnd(true);
            } else {

                if (mCurrentCounter >= mData.getTotal()) {
                    mAdapter.loadMoreEnd(mLoadMoreEndGone);
                } else {
                    if (!isErr) {
                        pageIndex++;
                        mPresenter.getHallList(pageIndex,mSort,mBetType);

                    }
                }

            }

    }

    @Override
    public void onClick(View v) {
        pageIndex = 0;
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_hall_new:
                mSort=3;
                mViewLeft.setVisibility(View.VISIBLE);
                mViewCenter.setVisibility(View.GONE);
                mViewRight.setVisibility(View.GONE);

                break;
            case R.id.rb_hall_up:
                mSort=2;
                mViewLeft.setVisibility(View.GONE);
                mViewCenter.setVisibility(View.VISIBLE);
                mViewRight.setVisibility(View.GONE);
                break;

            case R.id.rb_hall_down:
                mSort=1;
                mViewLeft.setVisibility(View.GONE);
                mViewCenter.setVisibility(View.GONE);
                mViewRight.setVisibility(View.VISIBLE);
                break;
        }

        initData();
    }
}
